import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

class StudentGrade {
    String name;
    String id;
    String course;
    double grade;

    public StudentGrade(String name, String id, String course, double grade) {
        this.name = name;
        this.id = id;
        this.course = course;
        this.grade = grade;
    }
}

public class StudentGradeManagementSystem {
    // 存储所有学生成绩的列表
    private static List<StudentGrade> grades = new ArrayList<>();
    // 用于从控制台读取用户输入的 Scanner 对象
    private static Scanner scanner = new Scanner(System.in);
    // 定义菜单选项的常量
    private static final int RECORD_GRADE = 1;
    private static final int QUERY_GRADE = 2;
    private static final int STATISTICS_GRADE = 3;
    private static final int EXIT_SYSTEM = 4;
    // 定义成绩范围的常量
    private static final int MIN_GRADE = 0;
    private static final int MAX_GRADE = 100;

    public static void main(String[] args) {
        while (true) {
            // 显示菜单
            displayMenu();
            // 获取用户输入的有效选项
            int choice = getValidChoice(RECORD_GRADE, EXIT_SYSTEM);
            switch (choice) {
                case RECORD_GRADE:
                    // 记录学生成绩
                    recordGrade();
                    break;
                case QUERY_GRADE:
                    // 查询学生成绩
                    queryGrade();
                    break;
                case STATISTICS_GRADE:
                    // 统计课程成绩
                    statisticsGrade();
                    break;
                case EXIT_SYSTEM:
                    System.out.println("感谢使用学生成绩管理系统，再见！");
                    scanner.close();
                    return;
            }
        }
    }

    private static void displayMenu() {
        System.out.println("=================================");
        System.out.println("欢迎使用学生成绩管理系统");
        System.out.println("=================================");
        System.out.println("请选择操作：");
        System.out.println("1. 记录学生成绩");
        System.out.println("2. 查询学生成绩");
        System.out.println("3. 统计课程成绩");
        System.out.println("4. 退出系统");
        System.out.print("请输入选项序号：");
    }

    private static int getValidChoice(int min, int max) {
        int choice;
        while (true) {
            try {
                choice = scanner.nextInt();
                scanner.nextLine();
                if (choice >= min && choice <= max) {
                    break;
                } else {
                    System.out.println("输入无效，请输入 " + min + " 到 " + max + " 之间的数字。");
                }
            } catch (Exception e) {
                System.out.println("输入无效，请输入有效的数字。");
                scanner.nextLine();
            }
        }
        return choice;
    }

    private static void recordGrade() {
        System.out.println("===== 记录学生成绩 =====");
        System.out.print("请输入学生姓名：");
        String name = scanner.nextLine();
        System.out.print("请输入学生学号：");
        String id = scanner.nextLine();
        if (isDuplicateId(id)) {
            System.out.println("错误：该学号已存在，请重新操作。");
            return;
        }
        System.out.print("请输入课程名称：");
        String course = scanner.nextLine();
        double grade = getValidGrade();
        grades.add(new StudentGrade(name, id, course, grade));
        System.out.println("成绩已成功记录！");
    }

    private static boolean isDuplicateId(String id) {
        for (StudentGrade grade : grades) {
            if (grade.id.equals(id)) {
                return true;
            }
        }
        return false;
    }

    private static double getValidGrade() {
        double grade;
        while (true) {
            try {
                System.out.print("请输入成绩（0 - 100）：");
                grade = scanner.nextDouble();
                scanner.nextLine();
                if (grade >= 0 && grade <= 100) {
                    break;
                } else {
                    System.out.println("错误：成绩必须在 0 到 100 之间，请重新输入。");
                }
            } catch (Exception e) {
                System.out.println("输入无效，请输入有效的数字。");
                scanner.nextLine();
            }
        }
        return grade;
    }

    private static void queryGrade() {
        System.out.println("===== 查询学生成绩 =====");
        System.out.println("请选择查询方式：");
        System.out.println("1. 按学生姓名查询");
        System.out.println("2. 按学生学号查询");
        System.out.println("3. 按课程名称查询");
        int queryChoice = getValidChoice(1, 3);
        System.out.print("请输入查询关键字：");
        String keyword = scanner.nextLine();
        boolean found = false;
        for (StudentGrade grade : grades) {
            switch (queryChoice) {
                case 1:
                    if (grade.name.equals(keyword)) {
                        printGrade(grade);
                        found = true;
                    }
                    break;
                case 2:
                    if (grade.id.equals(keyword)) {
                        printGrade(grade);
                        found = true;
                    }
                    break;
                case 3:
                    if (grade.course.equals(keyword)) {
                        printGrade(grade);
                        found = true;
                    }
                    break;
            }
        }
        if (!found) {
            System.out.println("未找到相关成绩记录。");
        }
    }

    private static void printGrade(StudentGrade grade) {
        System.out.printf("姓名：%s, 学号：%s, 课程：%s, 成绩：%.1f\n", grade.name, grade.id, grade.course, grade.grade);
    }

    private static void statisticsGrade() {
        System.out.println("===== 统计课程成绩 =====");
        System.out.print("请输入课程名称：");
        String course = scanner.nextLine();
        int count = 0;
        double sum = 0;
        double max = Double.MIN_VALUE;
        double min = Double.MAX_VALUE;
        for (StudentGrade grade : grades) {
            if (grade.course.equals(course)) {
                count++;
                sum += grade.grade;
                if (grade.grade > max) {
                    max = grade.grade;
                }
                if (grade.grade < min) {
                    min = grade.grade;
                }
            }
        }
        if (count == 0) {
            System.out.println("未找到该课程的成绩记录。");
        } else {
            double average = sum / count;
            System.out.printf("课程：%s\n", course);
            System.out.printf("平均分：%.2f\n", average);
            System.out.printf("最高分：%.1f\n", max);
            System.out.printf("最低分：%.1f\n", min);
        }
    }
}    